<template>
	<view class="listContainer">
		<view class="searchContainer">
			<uni-search-bar placeholder="请输入您想查看的机构" bgColor="#FFFFFF" placeholderColor="#D8D8D8" radius=18
				@confirm="searchHadle()" cancel-text="取消">
				<uni-icons slot="searchIcon" color="#D8D8D8" size="18" type="search" />
			</uni-search-bar>
		</view>
		<view class="screenContainer" v-if="screenList">
			<view class="screenHead">
				<view :class="['screenItem',{'ac':screenItemIndex!==null}]" @click="screenHadle()" >
					{{screenList.name}}
					<view  :class="['icon',{'active':screenItemIndex!==null}]" ></view>
				</view>
			</view>
		</view>
		<view v-if="list.length===0">
			<luanqing-empty :show="true" text="没有更多数据啦" textColor="#000"></luanqing-empty>
		</view>
		<view class="list" v-for="(item,index) in list" :key='index'>
			<!-- :url="'../course/list?cate_id='+cate_id+'&shop_id='+item.id" -->
			<navigator :url="'../stage/class?id='+item.id" hover-class="none">
				<view class="content">
					<view class="pic">
						<image :src="item.image" mode=""></image>
					</view>
					<view class="detail">
						<text class="title">{{item.name}}</text>
						<view class="local" v-if="item.address">
							<image src="../../static/img/dizhi.png" mode=""></image>
							<text>机构地址：{{item.address}}</text>
						</view>
						<view class="phone" v-if="item.tel">
							<image src="../../static/img/dianhua.png" mode=""></image>
							<text>机构电话：{{item.tel}}</text>
						</view>
					</view>
				</view>
				<view class="teacherIntro">
					<view class="avatar">
						<image :src="item.contact_image" mode=""></image>
					</view>
					<view class="intro">
						<view class="teacherName">
							<text>{{item.contact_name}}</text>
						</view>
						<view class="dianhua" v-if="item.contact_phone">
							<text>电话：{{item.contact_phone}}</text>
						</view>
					</view>
					<view class="juli">
						{{item.distance}}
					</view>
				</view>
			</navigator>
		</view>
		<uni-popup ref="popup" type="top">
			<view class="headContainer">
				<view class="searchContainer">
					<uni-search-bar placeholder="请输入您想查看的机构" bgColor="#FFFFFF" placeholderColor="#D8D8D8" radius=18
						@confirm="searchHadle()" cancel-text="取消">
						<uni-icons slot="searchIcon" color="#D8D8D8" size="18" type="search" />
					</uni-search-bar>
				</view>
				<view class="screenContainer">
					<view class="screenHead">
						<view class="screenItem" @click="screenHadle()">
							{{screenList.name}}
							<view class="icon"></view>
						</view>
					</view>
					<view class="screenContent">
						<view :class="['childItem',{'childItemAc':screenItemIndex===index}]" 
						v-for="(item,index) in screenList.child" 
						@click="screenItemHadle(item,index)"
						:key="index">
							{{item.name}}
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				page: 1,
				pagesize: 20,
				loadStatus: 'loading', //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
				isLoadMore: false, //是否加载中
				token: "",
				cate_id: "",
				search: "",
				screenList: '',
				screenIndex: null,
				screenItemIndex:null,
				distance:"",
			}
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			console.log(option); //打印出上个页面传递的参数。
			this.cate_id = option.cate_id?option.cate_id:"";
			uni.showLoading({
				title:"加载中...",
				mask:true
			})
			uni.getStorage({
				key: 'user_info',
				success: (res) => {
					this.token = res.data.token;
					console.log(res.data.token);
					this.getlist();
				},
				fail:()=> {
					this.init();
				}
			});
			this.getSchoolListTag();
		},
		onReachBottom() { //上拉触底函数
			if (!this.isLoadMore) { //此处判断，上锁，防止重复请求
				this.isLoadMore = true
				this.page += 1
				this.getlist()
			}
		},
		methods: {
			searchHadle(e) {
				this.search = e.value;
				this.list = [];
				this.getlist();
			},
			screenHadle(index) {
				this.$refs.popup.open();
			},
			screenItemHadle(obj,index){
				this.screenItemIndex = index;
				this.distance = obj.value;
				this.list = [];
				this.getlist();
				this.$refs.popup.close();
			},
			getSchoolListTag() {
				uni.request({
					url: `https://mx.acktechnologies.com.cn/api/wxapp/apply/getSchoolListTag`,
					method: 'GET',
					success: res => {
						if (res.data.code === 1) {
							this.screenList = res.data.data;
						}
					}
				})
			},
			getlist() {
				uni.request({
					url: `https://mx.acktechnologies.com.cn/api/wxapp/apply/getSchoolList`,
					data: {
						"page": this.page,
						"pageSize": 20,
						"search": this.search,
						"distance":this.distance,
						'cate_id':this.cate_id,
						"token":this.token
					},
					method: 'GET',
					success: res => {
						console.log(res)
						if (res.data.code == 1) {
							uni.hideLoading();
							if (res.data.data) {
								this.list = this.list.concat(res.data.data)
								if (res.data.data.length < this.pagesize) { //判断接口返回数据量小于请求数据量，则表示此为最后一页
									this.isLoadMore = true
									this.loadStatus = 'nomore'
								} else {
									this.isLoadMore = false
								}
							} else {
								this.isLoadMore = true
								this.loadStatus = 'nomore'
							}
						} else { //接口请求失败的处理
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							})
							this.isLoadMore = false
							if (this.page > 1) {
								this.page -= 1
							}
						}
					},
					fail: () => { //接口请求失败的处理
						uni.showToast({
							title: '服务器开小差了呢，请您稍后再试',
							icon: 'none'
						})
						this.isLoadMore = false
						if (this.page > 1) {
							this.page -= 1
						}
					},
				});
			},
		}
	}
</script>

<style scoped lang="scss">
	.headContainer {
		background-color: #FFFFFF;
		overflow: hidden;
	}
	.listContainer{
		padding-bottom: 20px;
	}
	.list {
		width: 670rpx;
		height: 362rpx;
		background: #FFFFFF;
		box-shadow: 0px 4rpx 20rpx 0px #E9EBF6;
		border-radius: 16rpx;
		padding: 20rpx;
		margin: 20rpx auto;

		.content {
			width: 628rpx;
			height: 240rpx;
			margin: 0 auto;
			border-bottom: 1px solid #EBEBEB;
			box-sizing: border-box;
			display: flex;
			flex-direction: row;
			align-items: center;

			.pic {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
				flex-shrink: 0;

				image {
					display: block;
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}

			.detail {
				width: 374rpx;
				flex-shrink: 0;
				margin-left: 20rpx;

				.title {
					font-size: 32rpx;
					font-weight: 700;
					color: #333333;
					line-height: 24rpx;
				}

				.local {
					margin-top: 16rpx;
				}

				.local,
				.phone {
					display: flex;
					align-items: flex-start;

					image {
						width: 26rpx;
						height: 30rpx;
						margin: 6rpx 8rpx 0 0;
						flex-shrink: 0;
					}

					text {
						font-size: 26rpx;
						font-family: SourceHanSansCN-Normal, SourceHanSansCN;
						font-weight: 400;
						color: #666666;
						line-height: 36rpx;
					}
				}

				.phone {
					margin-top: 8rpx;
				}
			}
		}

		.teacherIntro {
			height: 80rpx;
			margin-top: 22rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;

			.avatar {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;

				image {
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
				}
			}

			.intro {
				width: 460rpx;
				padding-left: 20rpx;
				box-sizing: border-box;

				.teacherName {
					font-size: 30rpx;
					font-family: SourceHanSansCN-Normal, SourceHanSansCN;
					font-weight: 400;
					color: #666666;
				}

				.dianhua {
					font-size: 26rpx;
					font-family: SourceHanSansCN-Normal, SourceHanSansCN;
					font-weight: 400;
					color: #999999;
					margin-top: 10rpx;
				}
			}

			.juli {
				font-size: 30rpx;
				font-family: SourceHanSansCN-Normal, SourceHanSansCN;
				font-weight: 400;
				color: #999999;
				text-align: right;
			}
		}
	}
	.screenContainer {
		padding: 0 40rpx;
		margin-bottom: 20px;
		box-sizing: border-box;
	
		.screenHead {
			width: 670rpx;
			margin: 0 auto;
			display: flex;
			flex-direction: row;
			justify-content: flex-start;
			align-items: center;
			margin: 5px 0;
	
			.screenItem {
				margin-right: 40rpx;
				display: flex;
				flex-direction: row;
				justify-content: flex-start;
				align-items: baseline;
				font-size: 15px;
				color: #666666;
				line-height: 15px;
	
				.icon {
					width: 0;
					height: 0;
					border-bottom: 6px solid #666666;
					border-left: 6px solid transparent;
					margin-left: 3px;
				}
	
				.active {
					width: 0;
					height: 0;
					border-bottom: 6px solid #48D28C;
					border-left: 6px solid transparent;
					margin-left: 3px;
				}
			}
			.ac{
				color: #48D28C;
			}
		}
	
		.childItem {
			display: inline-block;
			font-size: 15px;
			line-height: 15px;
			color: #666666;
			padding: 4px 12px;
			border-radius: 13px;
			background: #F6F6F6;
			margin-right: 20px;
			margin-top: 20px;
			box-sizing: border-box;
			border: 1px solid #F6F6F6;
		}
		.childItemAc{
			background-color: #FFFFFF;
			border: 1px solid #48D28C;
		}
	}
</style>
